package com.yuuzy.view;

import com.yuuzy.dao.InsuranceDAO;
import com.yuuzy.model.Insurance;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class ManageInsurance extends JInternalFrame {
    private JTable insurance_table;

    private InsuranceDAO insuranceDAO = new InsuranceDAO();
    private JTextField QI_nameText;
    private JTextField insurance_idText;
    private JTextField insurance_nameText;
    private JTextField insurance_projectText;
    private JTextField insurance_personText;
    private JTextField insurance_amountText;
    private JTextField insurance_yearText;
    private JRadioButton insurance_upRB;
    private JRadioButton insurance_downRB;
    private ButtonGroup group = new ButtonGroup();


    /**
     * Create the frame.
     */
    public ManageInsurance() {
        getContentPane().setFont(new Font("宋体", Font.PLAIN, 18));
        setClosable(true);
        setIconifiable(true);
        setTitle("保险产品管理");
        setBounds(100, 100, 1000, 1100);

        JScrollPane scrollPane = new JScrollPane();

        JLabel label = new JLabel("保险名称：");
        label.setFont(new Font("宋体", Font.PLAIN, 20));

        QI_nameText = new JTextField();
        QI_nameText.setFont(new Font("宋体", Font.PLAIN, 20));
        QI_nameText.setColumns(10);

        JButton btnNewButton = new JButton("查询 ");
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        btnNewButton.setIcon(new ImageIcon(ManageInsurance.class.getResource("/image/pm-query-16.png")));

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4FEE\u6539/\u5220\u9664\u4FDD\u9669\u4EA7\u54C1", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(153)
                                                .addComponent(label)
                                                .addGap(48)
                                                .addComponent(QI_nameText, GroupLayout.PREFERRED_SIZE, 264, GroupLayout.PREFERRED_SIZE)
                                                .addGap(76)
                                                .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(89)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 807, GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap(87, GroupLayout.PREFERRED_SIZE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(56)
                                                .addComponent(label))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(50)
                                                .addComponent(btnNewButton))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(53)
                                                .addComponent(QI_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addGap(68)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 241, GroupLayout.PREFERRED_SIZE)
                                .addGap(65)
                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 560, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(42, Short.MAX_VALUE))
        );

        JLabel label_1 = new JLabel("保险编号：");

        insurance_idText = new JTextField();
        insurance_idText.setFont(new Font("宋体", Font.PLAIN, 18));
        insurance_idText.setEnabled(true);
        insurance_idText.setEditable(false);
        insurance_idText.setText("");
        insurance_idText.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("保险名称：");

        insurance_nameText = new JTextField();
        insurance_nameText.setColumns(10);

        JLabel label_2 = new JLabel("保险项目：");

        insurance_projectText = new JTextField();
        insurance_projectText.setColumns(10);

        JLabel label_3 = new JLabel("适用人群：");

        insurance_personText = new JTextField();
        insurance_personText.setColumns(10);

        JLabel label_4 = new JLabel("保险价格：");

        insurance_amountText = new JTextField();
        insurance_amountText.setColumns(10);

        JLabel label_5 = new JLabel("保险年限：");

        insurance_yearText = new JTextField();
        insurance_yearText.setColumns(10);

        JLabel label_6 = new JLabel("是否上线：");

        insurance_upRB = new JRadioButton("是");
        group.add(insurance_upRB);
        insurance_upRB.setSelected(true);

        insurance_downRB = new JRadioButton("否");
        group.add(insurance_downRB);

        JButton btnNewButton_1 = new JButton("修改 ");
        btnNewButton_1.setIcon(new ImageIcon(ManageInsurance.class.getResource("/image/icon_update.png")));
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateActionPerformed(e);
            }
        });

        JButton btnNewButton_2 = new JButton("删除 ");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteActionPerformed(e);
            }
        });
        btnNewButton_2.setIcon(new ImageIcon(ManageInsurance.class.getResource("/image/delete.png")));
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(75)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(label_1)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(insurance_idText, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(lblNewLabel_1)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(insurance_nameText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                                .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                                .addComponent(insurance_yearText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                                .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                                .addComponent(insurance_personText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))))))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(179)
                                                .addComponent(btnNewButton_1)))
                                .addGap(54)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(insurance_projectText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
                                                        .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE))
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(insurance_amountText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(btnNewButton_2))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addGap(18)
                                                                .addComponent(insurance_upRB)
                                                                .addGap(48)
                                                                .addComponent(insurance_downRB, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)))))
                                .addContainerGap(81, Short.MAX_VALUE))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(71)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_1)
                                        .addComponent(insurance_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(insurance_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_2)
                                        .addComponent(insurance_projectText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(65)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_3)
                                        .addComponent(insurance_personText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_4)
                                        .addComponent(insurance_amountText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(69)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_5)
                                        .addComponent(insurance_yearText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_6)
                                        .addComponent(insurance_upRB)
                                        .addComponent(insurance_downRB))
                                .addPreferredGap(ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton_1)
                                        .addComponent(btnNewButton_2))
                                .addGap(43))
        );
        panel.setLayout(gl_panel);

        insurance_table = new JTable();
        insurance_table.setFillsViewportHeight(true);
        insurance_table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                tableMousePressed(me);
            }
        });
        insurance_table.setFont(new Font("宋体", Font.PLAIN, 16));
        insurance_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "保险编号", "保险名称", "保险项目", "适用人群", "保险金额", "保险年限", "上线标志位"
                }
        ) {
            boolean[] columnEditables = new boolean[]{
                    false, true, true, true, true, true, true
            };

            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        insurance_table.getColumnModel().getColumn(0).setPreferredWidth(119);
        insurance_table.getColumnModel().getColumn(1).setPreferredWidth(136);
        insurance_table.getColumnModel().getColumn(2).setPreferredWidth(165);
        insurance_table.getColumnModel().getColumn(3).setPreferredWidth(157);
        insurance_table.getColumnModel().getColumn(4).setPreferredWidth(156);
        insurance_table.getColumnModel().getColumn(5).setPreferredWidth(137);
        insurance_table.getColumnModel().getColumn(6).setPreferredWidth(118);
        scrollPane.setViewportView(insurance_table);
        getContentPane().setLayout(groupLayout);

        this.fillTable(new Insurance());
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ManageInsurance frame = new ManageInsurance();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 删除事件处理
     *
     * @param evt
     */
    private void deleteActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String insurance_id = insurance_idText.getText();
        if (StringUtils.isEmpty(insurance_id)) {
            JOptionPane.showMessageDialog(null, "请选择要删除的保险产品！");
            return;
        }
        Insurance insurance = new Insurance(StringUtils.changeInt(insurance_id));
        int n = JOptionPane.showConfirmDialog(null, "确定要删除该产品吗？");
        if (n == 0) {
            int deleteNum = insuranceDAO.deleteInsurance(insurance);
            if (deleteNum > 0) {
                JOptionPane.showMessageDialog(null, "删除成功！");
                this.resetValue();
                this.fillTable(new Insurance());
            } else {
                JOptionPane.showMessageDialog(null, "删除失败！");
            }
        }
    }

    /**
     * 修改事件处理
     *
     * @param evt
     */
    private void updateActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String insurance_id = this.insurance_idText.getText();
        if (StringUtils.isEmpty(insurance_id)) {
            JOptionPane.showMessageDialog(null, "请选择要修改的保险产品！");
            return;
        }

        String insurance_name = this.insurance_nameText.getText();
        String insurance_project = this.insurance_projectText.getText();
        String insurance_person = this.insurance_personText.getText();
        String insurance_amount = this.insurance_amountText.getText();
        String insurance_year = this.insurance_yearText.getText();

        if (StringUtils.isEmpty(insurance_name)) {
            JOptionPane.showMessageDialog(null, "保险名称不能为空！");
            return;
        }

        if (StringUtils.isEmpty(insurance_project)) {
            JOptionPane.showMessageDialog(null, "保险项目不能为空！");
            return;
        }

        if (StringUtils.isEmpty(insurance_person)) {
            JOptionPane.showMessageDialog(null, "适用人群不能为空！");
            return;
        }

        if (StringUtils.isEmpty(insurance_amount)) {
            JOptionPane.showMessageDialog(null, "保险金额不能为空！");
            return;
        }

        if (StringUtils.isEmpty(insurance_year)) {
            JOptionPane.showMessageDialog(null, "保险年限不能为空！");
            return;
        }

        String insurance_up = "";
        if (insurance_upRB.isSelected()) {
            insurance_up = "1";
        } else if (insurance_downRB.isSelected()) {
            insurance_up = "0";
        }

        if (StringUtils.isEmpty(insurance_up)) {
            JOptionPane.showMessageDialog(null, "请选择是否上线该保险！");
            return;
        }

        Insurance insurance = new Insurance(StringUtils.changeInt(insurance_id), insurance_name, insurance_project, insurance_person,
                StringUtils.changeDouble(insurance_amount), StringUtils.changeInt(insurance_year), insurance_up);
        int insertNum = insuranceDAO.updateInsurance(insurance);
        if (insertNum > 0) {
            JOptionPane.showMessageDialog(null, "修改成功！");
            this.resetValue();
            this.fillTable(new Insurance());
        } else {
            JOptionPane.showMessageDialog(null, "修改失败！");
        }

    }

    //重置表单
    private void resetValue() {
        // TODO Auto-generated method stub
        this.insurance_idText.setText("");
        this.insurance_nameText.setText("");
        this.insurance_projectText.setText("");
        this.insurance_personText.setText("");
        this.insurance_amountText.setText("");
        this.insurance_yearText.setText("");
        this.group.clearSelection();
    }

    /**
     * 表格行点击事件处理
     *
     * @param tme
     */
    private void tableMousePressed(MouseEvent tme) {
        // TODO Auto-generated method stub
        int row = this.insurance_table.getSelectedRow();
        this.insurance_idText.setText(String.valueOf(insurance_table.getValueAt(row, 0)));
        this.insurance_nameText.setText(String.valueOf(insurance_table.getValueAt(row, 1)));
        this.insurance_projectText.setText(String.valueOf(insurance_table.getValueAt(row, 2)));
        this.insurance_personText.setText(String.valueOf(insurance_table.getValueAt(row, 3)));
        this.insurance_amountText.setText(String.valueOf(insurance_table.getValueAt(row, 4)));
        this.insurance_yearText.setText(String.valueOf(insurance_table.getValueAt(row, 5)));
        String up = String.valueOf(insurance_table.getValueAt(row, 6));
        if ("1".equals(up)) {
            this.insurance_upRB.setSelected(true);
        } else if ("0".equals(up)) {
            this.insurance_downRB.setSelected(true);
        }
    }

    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String insurance_name = this.QI_nameText.getText();
        Insurance insurance = new Insurance();
        insurance.setInsurance_name(insurance_name);
        this.fillTable(insurance);
    }

    /**
     * 初始化表格
     *
     * @param insurance
     */
    private void fillTable(Insurance insurance) {
        DefaultTableModel dtm = (DefaultTableModel) insurance_table.getModel();
        dtm.setRowCount(0);//设置为0行，清空表格
        List<Insurance> insuranceList = insuranceDAO.queryInsurance(insurance);
        for (Insurance rs : insuranceList) {
            Vector v = new Vector();
            v.add(rs.getInsurance_id());
            v.add(rs.getInsurance_name());
            v.add(rs.getInsurance_project());
            v.add(rs.getInsurance_person());
            v.add(rs.getInsurance_amount());
            v.add(rs.getInsurance_year());
            v.add(rs.getInsurance_up());
            dtm.addRow(v);
        }
    }
}
